import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== global declare
const { $Lazy, $fn, $http } = window
// ===================================================================== Lazy
const Page 	 		= $Lazy.load(()=>import('#tp/page/page-container'))
const Tips 			= $Lazy.load(()=>import('#tp/tips'))
const confirm 		= $Lazy.file(()=>import('@antd/confirm'))
const message 		= $Lazy.file(()=>import('@antd/message'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const ModPriceModal = $Lazy.state(()=>import('./cpt/mod-price-modal'))
// ===================================================================== component
// 新增价格类型 2021-9-16 9:23:39 ?F10: AM? --伍智睿
const priceData = [{ name:'标准价格', value: '0' }, { name:'结算价格', value: '1' }]
class Index extends React.Component{
	state = {
		formData : [
			{ label: '项目',		name: 'kind_id', 				half:true,		required:true,	type: 'select', onValue:({ rows })=>{
				this.kindName = rows ?  rows.name : ''
			} },
			{ label: '医院',		name: 'hosp_id', 				half:true,		type: 'select', br:true },
			{ label: '账单生成时间',		names: ['start_time','end_time'], 	half:true,		type: 'date-range', br:true, name:'jz-time' },
			{ label: '修改价格',		name: 'price', 						half:true,	 type:'number',	required:true, br:true, onValue:({value})=>{
				this.setState({ price: value })
			} },
			{ label: '价格类型',		name: 'price_type', 			half:true,		type: 'radio',	data: priceData, br:true, value: "1" },
		]
	}
	
	componentDidMount(){
		this.getSelect()
	}
	// 获取下拉数据
	getSelect = async () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 医院
			$fn.setRow(formData, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			// 项目
			$fn.setRow(formData, 'kind_id', await f.getCache({ api:'specimenkindSelect' } ) )
			
			this.setState({ formData })
		})
		
	}
	
	render(){
		const { price } = this.state
		return (
			<>
				<Page title='修改项目价格'>
					<div className='ex pt20 mlr20 fv'>
						{ SubmitForm.call(this,{ 
							msg:'修改项目价格', okText:'确定修改', btnSize:'large', noEsc:true, hideButton:false, scroll:true, labelWidth:'s6', enter: false,
							children: (
								!isNaN(+price) ? <div className='fxm f12 w'>
									<Tips text='修改后，该项目的价格将会统一修改为'/><b className="c0"> &nbsp;{price} </b>
								</div> : null
							),
							onSubmit: param =>{
								const time = param['jz-time']
								param = { ...param, start_time: time?.[0], end_time: time?.[1], kindName:this.kindName }
								delete param['jz-time']
								
								$http.submit(null,'bill/getModifyBillCount', { param }).then(({ billCount })=>{
									if(billCount){
										confirm.then(f=>{
											f({
												msg: `是否将【${this.kindName}】  的价格修改为 【${price}】 元？本次操作影响的主条码数为 【${billCount}】？`,
												onOk: close => {
													close()
													this.modPriceModalRef.open(param)
												}
											})
										})
									}else{
										message.then(f => f.error('未找到数据'))
									}
								})
							}
						}) }
					</div>
				</Page>
				<ModPriceModal onRef={ref=>this.modPriceModalRef=ref} onRefresh={()=>{
					// this.formRef.reset(['price'])
					// this.setState({price:null})
				}} />
			</>
		)
	}
}

export default Index